package com.yilin.tms.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yilin.tms.core.commons.utils.QueryUtil;
import com.yilin.tms.core.entity.user.UserType;
import com.yilin.tms.core.entity.user.organize.MemberType;
import com.yilin.tms.core.entity.user.organize.Organize;
import com.yilin.tms.user.service.IMemberTypeService;
import com.yilin.tms.user.service.IOrganizeService;
import com.yilin.tms.user.mapper.MemberTypeMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;


@Service
public class MemberTypeService extends ServiceImpl<MemberTypeMapper, MemberType> implements IMemberTypeService {

    @Resource
    MemberTypeMapper memberTypeMapper;

    @Resource
    IOrganizeService organizeService;

    /**
     * 获取会员类型列表
     * */
    @Override
    public List<MemberType> getMemberTypeListByOrganizeId(String organizeId,MemberType query) {
        QueryWrapper<MemberType> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("organize_id", organizeId);
        QueryUtil.andQueryWrapper(queryWrapper,query);
        return memberTypeMapper.selectList(queryWrapper);
    }

    /**
     * 获取指定会员类型
     * */
    @Override
    public MemberType getMemberTypeById(String organizeId, String memberTypeId) {
        QueryWrapper<MemberType> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("organize_id", organizeId);
        queryWrapper.eq("id", memberTypeId);
        return memberTypeMapper.selectOne(queryWrapper);
    }

    /**
     * 获取我的会员类型
     * */
    @Override
    public MemberType getMyMemberType(String organizeId) {
        Organize organize = organizeService.getOrganizeById(organizeId);
        QueryWrapper<MemberType> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", organize.getMemberTypeId());
        return memberTypeMapper.selectOne(queryWrapper);
    }

    @Override
    public MemberType getMemberTypeByName(String organizeId, String name) {
        QueryWrapper<MemberType> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("organize_id", organizeId);
        queryWrapper.eq("name", name);
        return memberTypeMapper.selectOne(queryWrapper);
    }

    /**
     * 获取默认会员类型
     * */
    @Override
    public MemberType getDefaultMemberTypeByOrganizeId(String organizeId, UserType userType) {
        QueryWrapper<MemberType> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("organize_id", organizeId);
        queryWrapper.eq("user_type", userType);
        queryWrapper.eq("is_default", true);
        return memberTypeMapper.selectOne(queryWrapper);
    }

}
